﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace QLNS
{
    public partial class frmPhongban : Form
    {
        public frmPhongban()
        {
            InitializeComponent();
        }
        public SqlConnection getConnect()
        {
            return new SqlConnection(@"Data Source=THANGVUONG\SQLEXPRESS;Initial Catalog=Quanlynhansu;Integrated Security=True");
        }
        public DataTable Phongban_SelectAll()
        {
            string sql="Select * from PhongBan";
            SqlConnection conn = getConnect();
            SqlDataAdapter adap = new SqlDataAdapter(sql, conn);
            DataTable dt = new DataTable();
            adap.Fill(dt);
            return (dt);
        }
        public DataTable Phongban_Search(Phongban pb)
        {
            SqlConnection conn = getConnect();
            conn.Open();
            SqlCommand cmd = new SqlCommand("Phongban_Search", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@Maphong", pb.Maphong));
            SqlDataAdapter adap = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            adap.Fill(dt);
            conn.Close();
            return dt;
        }
        public void Phongban_Insert(Phongban pb)
        {
            SqlConnection Connect = getConnect();
            Connect.Open();
            SqlCommand cmd = new SqlCommand("Phongban_Insert", Connect);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@Maphong", pb.Maphong));
            cmd.Parameters.Add(new SqlParameter("@Tenphong", pb.Tenphong));
            cmd.ExecuteNonQuery();
            Connect.Close();
        }
        public void Phongban_Update(Phongban pb)
        {
            SqlConnection Connect = getConnect();
            Connect.Open();
            SqlCommand cmd = new SqlCommand("Phongban_Update", Connect);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@Maphong", pb.Maphong));
            cmd.Parameters.Add(new SqlParameter("@Tenphong", pb.Tenphong));
            cmd.ExecuteNonQuery();
            Connect.Close();
        }
        public void Phongban_Delete(string Maphong)
        {
            SqlConnection Connect = getConnect();
            Connect.Open();
            SqlCommand cmd = new SqlCommand("Phongban_Delete", Connect);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@Maphong", Maphong));
            cmd.ExecuteNonQuery();
            Connect.Close();
        }

        private void frmPhongban_Load(object sender, EventArgs e)
        {
            grvPB.DataSource = Phongban_SelectAll();
        }

        private void btThem_Click(object sender, EventArgs e)
        {
            if (txtMaphong.Text == "")
            {
                MessageBox.Show("Bạn phải nhập mã phòng!");
                txtMaphong.Focus();
                return;
            }
            Phongban PB = new Phongban();
            PB.Maphong = txtMaphong.Text;
            PB.Tenphong = txtTenphong.Text;
            try {
                Phongban_Insert(PB);
                grvPB.DataSource = Phongban_SelectAll();
            }
            catch { MessageBox.Show("Không thêm được!"); }
        }

        private void btSua_Click(object sender, EventArgs e)
        {
            if (txtMaphong.Text == "")
            {
                MessageBox.Show("Bạn phải nhập mã phòng!");
                txtMaphong.Focus();
                return;
            }
            Phongban PB = new Phongban();
            PB.Maphong = txtMaphong.Text;
            PB.Tenphong = txtTenphong.Text;
            try
            {
                Phongban_Update(PB);
                grvPB.DataSource = Phongban_SelectAll();
            }
            catch { MessageBox.Show("Không sửa được!"); }
        }

        private void btXoa_Click(object sender, EventArgs e)
        {
            if (txtMaphong.Text == "")
            {
                MessageBox.Show("Bạn phải nhập mã phòng!");
                txtMaphong.Focus();
                return;
            }
            Phongban_Delete(txtMaphong.Text);
            grvPB.DataSource = Phongban_SelectAll();
        }

        private void btThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtSearch_TextChanged(object sender, EventArgs e)
        {
            Phongban PB = new Phongban();
            PB.Maphong = txtSearch.Text;
            grvPB.DataSource = Phongban_Search(PB);
        }
    }
}
